import javax.swing.JFrame;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import java.util.Vector;
import java.util.regex.Pattern;

public class CodeAnalyse {

    public CodeAnalyse() {
        yufa mainFrame = new yufa();
        mainFrame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
        mainFrame.setVisible(true);
    }

    public static void main(String args[]) {
        new CodeAnalyse();
    }
}

class yufa extends JFrame {
    private static final long serialVersionUID = 1L;
    private JTextArea code = new JTextArea();
    private JTextArea reslut = new JTextArea();

    public yufa() {
        JTextArea jta;
        setTitle("代码分析器");
        Toolkit tk = Toolkit.getDefaultToolkit();
        Dimension screenSize = tk.getScreenSize();
        int WIDTH = 500;
        int HEIGHT = 625;
        setSize(500, 625);
        setLocation((screenSize.width - WIDTH) / 2, (screenSize.height - HEIGHT) / 2);

        MainPanel mainPanel = new MainPanel();
        add(mainPanel);
        code.getText();
        code.setText("int main()\n{\n   int a;\n   int b;\n   a=0;\n   b=1;\n   b=a+b+3;\n   b=a+b/3;\n   if(a>c)\n   {\n      a=c+d;\n      a=b;\n   }\n   else\n   {\n   b=a+3;\n   }\n   while(a>c)\n   {\n      a=c;\n   }\n}");

        /**主窗口关闭，程序即退出，添加次监听器，提示是否退出程序，保证数据安全*/
        addWindowListener(new MainWindowListener());
    }

    /**
     * 主界面窗口关闭监听器
     */
    class MainWindowListener extends WindowAdapter {
        public void windowClosing(WindowEvent e) {
            int close = JOptionPane.showConfirmDialog(null, "确定退出？",
                    "退出", JOptionPane.YES_NO_OPTION);
            if (close == JOptionPane.YES_OPTION) {
                System.exit(0);
            }
        }
    }

    /**
     * 类功能：
     * 主界面
     */
    class MainPanel extends JPanel {
        private static final long serialVersionUID = 1L;
        private JPanel basePanel = new JPanel();
        //		private JPanel baseChoosePanel = new JPanel();
        private JPanel resultPanel = new JPanel();
        private JButton chooseFileButton = new JButton("浏览文件...");
        private JButton analyse = new JButton("分析");
        private JScrollPane baseScrollPane;
        private JScrollPane resultScrollPane;

        public MainPanel() {
            setLayout(new GridLayout(2, 0));

            //显示要进行分析的代码
            basePanel.setLayout(new BorderLayout());
            baseScrollPane = new JScrollPane(code);
            basePanel.add(chooseFileButton, BorderLayout.NORTH);
            basePanel.add(baseScrollPane);

            //显示分析结果
            resultPanel.setLayout(new BorderLayout());

            analyse.addActionListener(new LexAnalyseButtonAction());

            resultScrollPane = new JScrollPane(reslut);

            resultPanel.add(analyse, BorderLayout.NORTH);
            resultPanel.add(resultScrollPane);

            add(basePanel);
            add(resultPanel);


        }
    }


    class LexAnalyseButtonAction extends AbstractAction {
        private static final long serialVersionUID = 1L;

        public void actionPerformed(ActionEvent arg0) {
            // TODO Auto-generated method stub
            // 判断有没有要分析的代码，没有代码提示输入代码
            if (code.getText().equals("")) {
                JOptionPane.showMessageDialog(null, "对不起，您没有要分析的代码！");
            } else {
                try {
                    String result = "";
                    a a1 = new a();
//                    result += a1.mainMethod(code.getText());
                    reslut.setText(result);
                    reslut.setFont(new Font(null, Font.BOLD, 14));
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }


}


class SYS {
    String fuhao;
    String op1;
    String op2;
    Node node;
}

class Node {
    String result;
    int num;
}

class a {
    Vector wait = new Vector();
    Vector wait1 = new Vector();
    Vector fenxizhan = new Vector();
    Vector Baoliuzi = new Vector();
    Vector jiefu = new Vector();
    Vector yunsuanfu = new Vector();
    Vector result = new Vector();

    int while_endsign = 0;
    static int count = 0;
    static int count_sign = 0;
    int if_sign = 0;
    int else_sign = 0;
    int while_sign = 10;
    int huitian[] = new int[50];
    int sign = 0;
    static SYS sys[] = new SYS[40];

    public String mainMethod(String sFromFrame, JTextArea jta) throws Exception {

        String returnString = "";
        a b = new a();
        for (int k = 0; k < 40; k++) {
            b.sys[k] = new SYS();
            sys[k].node = new Node();
        }
        String l = "";
        b.yunsuanfu.add("+");
        b.yunsuanfu.add("-");
        b.yunsuanfu.add("*");
        b.yunsuanfu.add("/");
        b.yunsuanfu.add(">");
        b.yunsuanfu.add("<");
        b.yunsuanfu.add("=");
        b.yunsuanfu.add("%");
        b.jiefu.add("(");
        b.jiefu.add(")");
        b.jiefu.add("{");
        b.jiefu.add("}");
        b.jiefu.add(";");
        b.Baoliuzi.add("else");
        b.Baoliuzi.add("int");
        b.Baoliuzi.add("if");
        b.Baoliuzi.add("while");
        b.Baoliuzi.add("char");
        b.Baoliuzi.add("float");
        b.Baoliuzi.add("double");
        b.Baoliuzi.add("main");
        String s = sFromFrame;
        boolean sign = false;
        String ls = "";
        for (int i = 0; i < s.length(); i++) {
            ls = s.substring(i, i + 1);
            l = l + ls;
            while ((ls.compareToIgnoreCase("a") >= 0) && (ls.compareToIgnoreCase("z") <= 0)) {
                sign = true;
                i++;
                if (i < s.length()) {
                    ls = s.substring(i, i + 1);
                }
                if (i == s.length() || ls.equals("}") || ls.equals("<") || ls.equals("{")
                        || ls.equals(">") || ls.equals(",") || ls.equals(";") || ls.equals("=")
                        || ls.equals("(") || ls.equals(")") || ls.equals(" ") || ls.equals("*")
                        || ls.equals("/") || ls.equals("+") || ls.equals("-")
                        || ls.equals("\n") || ls.equals("\t")) {
                    break;
                } else {
                    l = l + ls;
                }
            }
            if (isNum(ls)) {
                System.out.println("number " + ls);
                sign = true;
                while (true) {
                    i++;
                    ls = s.substring(i, i + 1);
                    if (ls.equals("}") || ls.equals(",") || ls.equals(";") || ls.equals("=")
                            || ls.equals("(") || ls.equals(")") || ls.equals(" ") || ls.equals("*")
                            || ls.equals("/") || ls.equals("\n") || ls.equals("\t")) {
                        break;
                    } else {
                        l = l + ls;
                    }
                }
            }
            if (isNum(l)) {
                b.wait.add(l);
                b.wait1.add(l);
                l = "";
                sign = false;
            } else if (b.Baoliuzi.contains(l)) {
                b.wait.add(l);
                b.wait1.add(l);
                l = "";
                sign = false;
            } else if (!b.Baoliuzi.contains(l) && sign == true) {
                b.wait.add(l);
                b.wait1.add(l);
                l = "";
                sign = false;
            }
            if (b.yunsuanfu.contains(ls) || b.jiefu.contains(ls)) {
                b.wait.add(ls);
                b.wait1.add(ls);
                l = "";
            }
            if (ls.equals(" ") || ls.equals("\n") || ls.equals("\t")) {
                l = "";
            }
        }
        b.wait.add("$");
        b.wait1.add("$");
        jta.append("**************************************************************\n");
        jta.append("**************************************************************\n");
        System.out.println("**************************************************************");
        System.out.println("**************************************************************");
        returnString += "lex begin ... ... ...\n";
        b.word();
        returnString += "lex psss !!!!!!\n";
        System.out.println("**************************************************************");
        System.out.println("**************************************************************");
        returnString += "LL1 begin ... ... ...\n";
        b.LL1(jta);
        returnString += "LL1 pass !!!!!!\n";
        returnString += "yuyi begin ... ... ...\n";
        b.four();
        for (int i = 0; i < huitian.length; i++) {
            System.out.println(huitian[i]);
        }
        returnString += "yuyi pass !!!!!!\n";
        returnString += "四元式结果如下：\n";
        jta.append("*******************************************************\n");
        jta.append("中间代码如下\n");
        System.out.println("四元式结果如下：");
        for (int i = 0; i < count; i++) {
            if ((sys[i].fuhao).equals("j")) {
                returnString += i + "\t(" + sys[i].fuhao + "," + sys[i].op1 + "," + sys[i].op2 + "," + sys[i].node.num + ")\n";
                System.out.println(i + "\t(" + sys[i].fuhao + "," + sys[i].op1 + "," + sys[i].op2 + "," + sys[i].node.num + ")");
                jta.append(i+1 + "\t(" + sys[i].fuhao + "," + sys[i].op1 + "," + sys[i].op2 + "," + sys[i].node.num + ")\n");
            } else if ((sys[i].fuhao).equals(">")) {
                returnString += i + "\t(" + sys[i].fuhao + "," + sys[i].op1 + "," + sys[i].op2 + "," + sys[i].node.num + ")\n";
                jta.append(i+1 + "\t(" + sys[i].fuhao + "," + sys[i].op1 + "," + sys[i].op2 + "," + sys[i].node.num + ")\n");
                System.out.println(i + "\t(" + sys[i].fuhao + "," + sys[i].op1 + "," + sys[i].op2 + "," + sys[i].node.num + ")");
            } else if ((sys[i].fuhao).equals("<")) {
                jta.append(i+1 + "\t(" + sys[i].fuhao + "," + sys[i].op1 + "," + sys[i].op2 + "," + sys[i].node.num + ")\n");
                returnString += i + "\t(" + sys[i].fuhao + "," + sys[i].op1 + "," + sys[i].op2 + "," + sys[i].node.num + ")\n";
                System.out.println(i + "\t(" + sys[i].fuhao + "," + sys[i].op1 + "," + sys[i].op2 + "," + sys[i].node.num + ")");
            } else {
                jta.append(i+1 + "\t(" + sys[i].fuhao + "," + sys[i].op1 + "," + sys[i].op2 + "," + sys[i].node.result + ")\n");
                returnString += i + "\t(" + sys[i].fuhao + "," + sys[i].op1 + "," + sys[i].op2 + "," + sys[i].node.result + ")\n";
                System.out.println(i + "\t(" + sys[i].fuhao + "," + sys[i].op1 + "," + sys[i].op2 + "," + sys[i].node.result + ")");
            }
        }
        jta.append("*******************************************************\n");
        jta.append("目标代码如下\n");
        for (int i = 0; i <count ; i++) {
            if ((sys[i].fuhao).equals("+")) {
                jta.append("MOV "+sys[i].op1+"X,"+sys[i].op1+"\n"+"ADD "+sys[i].op1+"X,"+sys[i].op2+"\n"+"MOV "+sys[i].node.num+","+sys[i].op1+"X"+"\n");
            }
            else if ((sys[i].fuhao).equals("-")) {
                jta.append("MOV "+sys[i].op1+"X,"+sys[i].op1+"\n"+"SUB "+sys[i].op1+"X,"+sys[i].op2+"\n"+"MOV "+ sys[i].node.num+","+sys[i].op1+"X"+"\n");
            }
            else if ((sys[i].fuhao).equals("/")){
                jta.append( "MOV "+sys[i].op1+"X,"+sys[i].op1+"\n"
                        +"SUB "+"DX,O"+"\n"
                        +"MOV "+sys[i].op1+","+sys[i].op1+"X"+"\n"
                        +"DIV "+sys[i].op1+"X"+"\n"
                        +"MOV T,"+sys[i].op1+"X"+"\n"
                );
            }
            else if((sys[i].fuhao).equals("<")){
                jta.append("MOV DX,1\n"+
                        "MOV "+sys[i].op1+"X,"+sys[i].op1+"\n"+
                        "CMP "+sys[i].op1+"X,"+sys[i].op2+"\n"+
                        "JB_LT\n"+ "MOV DX,0\n"+"_LT:MOV T,DX\n");
            }  else if((sys[i].fuhao).equals(">")){
                jta.append("MOV DX,1\n"+
                        "MOV "+sys[i].op1+"X,"+sys[i].op1+"\n"+
                        "CMP "+sys[i].op1+"X,"+sys[i].op2+"\n"+
                        "JA_LT\n"+ "MOV DX,0\n"+"_GT:MOV T,DX\n");
            }
            else if ((sys[i].fuhao).equals("j")){
                jta.append("JMP far ptr"+sys[i].node.num+"\n");
            }
        }
        return returnString;
    }

    //数的判断函数
    public boolean isNum(String name) {
        Pattern pattern = Pattern.compile("[0-9]*(\\.?)[0-9]*");
        if (pattern.matcher(name).matches()) {
            return true;
        }
        return false;
    }

    ////标识符和保留字的判断函数
    public boolean isBaoliuzi(String name) {
        int length = name.length();
        boolean q = false;
        for (int i = 0; i < length; i++) {
            String n = name.substring(i, i + 1);
            if ((n.compareToIgnoreCase("a") >= 0) && (n.compareToIgnoreCase("z") <= 0)) {
                i++;
                if (i < length) {
                    n = name.substring(i, i + 1);
                }
                while ((n.compareToIgnoreCase("a") >= 0) && (n.compareToIgnoreCase("z") <= 0)) {
                    i++;
                    if (i >= length) {
                        q = true;
                        break;
                    }
                    if (i < length) {
                        n = name.substring(i, i + 1);
                    }
                }
                if (i == length || n.equals(",") || n.equals(";") || n.equals("=") || n.equals("(") || n.equals(")")
                        || n.equals(" ") || n.equals("*") || n.equals("/") || n.equals("+") || n.equals("-")) {
                    q = true;
                }
            } else {
                return false;
            }
        }
        return q;
    }

    //判断是否是终结符
    public boolean ifReslut(String fenxizhan) {
        return fenxizhan.equals(">")
                || fenxizhan.equals("else")
                || fenxizhan.equals("<")
                || fenxizhan.equals("float")
                || fenxizhan.equals("char")
                || fenxizhan.equals("if")
                || fenxizhan.equals(",")
                || fenxizhan.equals("=")
                || fenxizhan.equals("while")
                || fenxizhan.equals(";")
                || fenxizhan.equals("+")
                || fenxizhan.equals("*")
                || fenxizhan.equals("(")
                || fenxizhan.equals(")")
                || fenxizhan.equals("-")
                || fenxizhan.equals("/")
                || fenxizhan.equals("main")
                || fenxizhan.equals("{")
                || fenxizhan.equals("}")
                || fenxizhan.equals("int")
                || fenxizhan.equals("void")
                || fenxizhan.equals("变量");
    }

    public void LL1(JTextArea jta) {
        fenxizhan.add("MAIN_IN");
        fenxizhan.add("$");
        Baoliuzi.add("if");
        Baoliuzi.add("else");
        Baoliuzi.add("int");
        Baoliuzi.add("char");
        Baoliuzi.add("float");
        Baoliuzi.add("double");
        Baoliuzi.add("string");
        String s = null;
        //state表示当前跳到得非终结符
        int state = 0, k, sign = 0, hang = 0;
        //judge判断保留字
        boolean t = true, judge = false, isnum = false;
        while (fenxizhan.size() > 0) {
            String fxz = (String) fenxizhan.elementAt(0);
            String s_wait = (String) wait.elementAt(0);
            String S = s_wait.substring(0, 1);
            if (isBaoliuzi(s_wait)) {
                judge = true;
            }
            if (isNum(s_wait)) {
                isnum = true;
            }
            if (fxz.equals("MAIN_IN"))
                state = 0;
            else if (fxz.equals("First "))
                state = 1;
            else if (fxz.equals("variable "))
                state = 2;
            else if (fxz.equals("Value"))
                state = 3;
            else if (fxz.equals("Value2"))
                state = 4;
            else if (fxz.equals("Other"))
                state = 5;
            else if (fxz.equals(" 表达式"))
                state = 6;
            else if (fxz.equals(" 分析栈的剩余字符"))
                state = 7;
            else if (fxz.equals("T"))
                state = 8;
            else if (fxz.equals(" 符号 "))
                state = 9;
            else if (fxz.equals("Fchar"))
                state = 10;
            if ((wait.elementAt(0).equals("$")) && (fenxizhan.elementAt(0).equals("$"))) {
                fenxizhan.removeElementAt(0);
                wait.removeElementAt(0);
                System.out.println("\n语法分析通过!");
                jta.append("\n语法分析通过!\n");
                break;
            }
            //if 处理终结符，else处理非终结符
            if (ifReslut(fxz)) {//终结符时的处理

                if (fxz.equals("else")
                        || fxz.equals(")")
                        || fxz.equals("{")
                        || fxz.equals("}")
                        || fxz.equals(";")) {

                    hang++;
                }
                if (isnum == true
                        || fxz.equals(s_wait)
                        || (fxz.equals("变量") && judge == true && !(Baoliuzi.contains(s_wait)))) {

                    fenxizhan.removeElementAt(0);
                    wait.removeElementAt(0);
                    judge = false;
                    isnum = false;
                } else {
                    System.out.println();
                    fenxizhan.removeElementAt(0);
                    wait.removeElementAt(0);
                    javax.swing.JOptionPane.showMessageDialog(null, "第" + (hang) + "行   语法错误（算数表达式错误）!");
                }
            } else//非终极符时的处理
            {
                switch (state) {
                    case 0: {
                        if (s_wait.equals("main")) {
                            s = (String) fenxizhan.elementAt(0);
                            fenxizhan.removeElementAt(0);
                            fenxizhan.add(0, "main");
                            fenxizhan.add(1, "(");
                            fenxizhan.add(2, ")");
                            fenxizhan.add(3, "{");
                            fenxizhan.add(4, "First ");
                            fenxizhan.add(5, "Other");
                            fenxizhan.add(6, "}");
                        } else if (s_wait.equals("int") || s_wait.equals("void")) {
                            s = (String) fenxizhan.elementAt(0);
                            fenxizhan.removeElementAt(0);
                            fenxizhan.add(0, s_wait);
                            fenxizhan.add(1, "main");
                            fenxizhan.add(2, "(");
                            fenxizhan.add(3, ")");
                            fenxizhan.add(4, "{");
                            fenxizhan.add(5, "First ");
                            fenxizhan.add(6, "Other");
                            fenxizhan.add(7, "}");
                        } else {
                            javax.swing.JOptionPane.showMessageDialog(null, "第" + hang + "行   error!");
                            System.exit(0);
                        }
                        break;
                    }
                    case 1: {
                        if (s_wait.equals("int")
                                || s_wait.equals("char")
                                || s_wait.equals("float")
                                || s_wait.equals("double")) {
                            s = (String) fenxizhan.elementAt(0);
                            fenxizhan.removeElementAt(0);
                            fenxizhan.add(0, "variable ");
                            fenxizhan.add(1, "Value");
                            fenxizhan.add(2, ";");
                        } else {
                            javax.swing.JOptionPane.showMessageDialog(null, "第" + hang + "行   发生语法错误（非法数据类型）!");
                            System.exit(0);
                            fenxizhan.removeElementAt(0);
                        }
                        break;
                    }
                    case 2: {
                        if (s_wait.equals("int")
                                || s_wait.equals("char")
                                || s_wait.equals("float")
                                || s_wait.equals("double")) {

                            s = (String) fenxizhan.elementAt(0);
                            fenxizhan.removeElementAt(0);
                            fenxizhan.add(0, s_wait);
                        } else {
                            javax.swing.JOptionPane.showMessageDialog(null, "第" + hang + "行   发生语法错误!");
                            System.exit(0);
                            fenxizhan.removeElementAt(0);
                        }
                        break;
                    }
                    case 3: {
                        if (judge == true) {
                            s = (String) fenxizhan.elementAt(0);
                            fenxizhan.removeElementAt(0);
                            fenxizhan.add(0, "变量");
                            fenxizhan.add(1, "Value2");
                        } else {
                            javax.swing.JOptionPane.showMessageDialog(null, "第" + hang + "行   发生语法错误!");
                            System.exit(0);
                            fenxizhan.removeElementAt(0);
                        }
                        break;
                    }
                    case 4: {
                        if (s_wait.equals(";")) {
                            s = (String) fenxizhan.elementAt(0);
                            fenxizhan.removeElementAt(0);
                        } else if (s_wait.equals(",")) {
                            s = (String) fenxizhan.elementAt(0);
                            fenxizhan.removeElementAt(0);
                            fenxizhan.add(0, ",");
                            fenxizhan.add(1, "变量");
                            fenxizhan.add(2, "Value2");
                        } else if (s_wait.equals("=")) {
                            s = (String) fenxizhan.elementAt(0);
                            fenxizhan.removeElementAt(0);
                            fenxizhan.add(0, "=");
                            fenxizhan.add(1, " 表达式");
                        } else if (s_wait.equals("$")) {
                            s = (String) fenxizhan.elementAt(0);
                            fenxizhan.removeElementAt(0);
                        } else {
                            javax.swing.JOptionPane.showMessageDialog(null, "第" + hang + "行   发生语法错误! ");
                            fenxizhan.removeElementAt(0);
                            System.exit(0);
                        }
                        break;
                    }
                    case 5: {
                        if (s_wait.equals("}")) {
                            s = (String) fenxizhan.elementAt(0);
                            fenxizhan.removeElementAt(0);
                        } else if (judge == true
                                && !(s_wait.equals("else")
                                || s_wait.equals("if")
                                || s_wait.equals("while")
                                || s_wait.equals("double")
                                || s_wait.equals("int")
                                || s_wait.equals("float")
                                || s_wait.equals("char"))) {

                            s = (String) fenxizhan.elementAt(0);
                            fenxizhan.removeElementAt(0);
                            fenxizhan.add(0, "变量");
                            fenxizhan.add(1, "=");
                            fenxizhan.add(2, " 表达式");
                            fenxizhan.add(3, ";");
                            fenxizhan.add(4, "Other");
                            judge = false;
                        } else if (s_wait.equals("double") || s_wait.equals("int") || s_wait.equals("float") || s_wait.equals("char")) {
                            s = (String) fenxizhan.elementAt(0);
                            fenxizhan.removeElementAt(0);
                            fenxizhan.add(0, "First ");
                            fenxizhan.add(1, "Other");
                        } else if (s_wait.equals("if") || s_wait.equals("while")) {
                            s = (String) fenxizhan.elementAt(0);
                            fenxizhan.removeElementAt(0);
                            fenxizhan.add(0, s_wait);
                            fenxizhan.add(1, "(");
                            fenxizhan.add(2, " 表达式");
                            fenxizhan.add(3, ")");
                            fenxizhan.add(4, "{");
                            fenxizhan.add(5, "Other");
                            fenxizhan.add(6, "}");
                            fenxizhan.add(7, "Other");
                        } else if (s_wait.equals("else")) {
                            s = (String) fenxizhan.elementAt(0);
                            fenxizhan.removeElementAt(0);
                            fenxizhan.add(0, s_wait);
                            fenxizhan.add(1, "{");
                            fenxizhan.add(2, "Other");
                            fenxizhan.add(3, "}");
                            fenxizhan.add(4, "Other");
                        } else if (s_wait.equals(";")) {
                            s = (String) fenxizhan.elementAt(0);
                            fenxizhan.removeElementAt(0);
                            fenxizhan.add(0, ";");
                            fenxizhan.add(1, "Other");
                        } else {
                            javax.swing.JOptionPane.showMessageDialog(null, "第" + hang + "行   error! 缺少或多余}符号");
                            fenxizhan.removeElementAt(0);
                            System.exit(0);
                        }
                        break;
                    }
                    case 6: {
                        if (judge == true || isnum == true) {
                            s = (String) fenxizhan.elementAt(0);
                            fenxizhan.removeElementAt(0);
                            fenxizhan.add(0, "T");
                            fenxizhan.add(1, " 分析栈的剩余字符");
                            judge = false;
                            isnum = false;
                        } else if (s_wait.equals("(")) {
                            s = (String) fenxizhan.elementAt(0);
                            fenxizhan.removeElementAt(0);
                            fenxizhan.add(0, "T");
                            fenxizhan.add(1, " 分析栈的剩余字符");
                            sign = 1;
                        } else {
                            javax.swing.JOptionPane.showMessageDialog(null, "第" + hang + "行   error!");
                            fenxizhan.removeElementAt(0);
                            System.exit(0);
                        }
                        break;
                    }
                    case 7: {
                        if (s_wait.equals("+") || s_wait.equals(">") || s_wait.equals("<")) {
                            s = (String) fenxizhan.elementAt(0);
                            fenxizhan.removeElementAt(0);
                            fenxizhan.add(0, s_wait);
                            fenxizhan.add(1, "T");
                            fenxizhan.add(2, " 分析栈的剩余字符");
                        } else if (s_wait.equals("-")) {
                            s = (String) fenxizhan.elementAt(0);
                            fenxizhan.removeElementAt(0);
                            fenxizhan.add(0, "-");
                            fenxizhan.add(1, "T");
                            fenxizhan.add(2, " 分析栈的剩余字符");
                        } else if (s_wait.equals(")")) {
                            s = (String) fenxizhan.elementAt(0);
                            fenxizhan.removeElementAt(0);
                        } else if (s_wait.equals(";")) {
                            s = (String) fenxizhan.elementAt(0);
                            fenxizhan.removeElementAt(0);
                        } else if (s_wait.equals("$")) {
                            s = (String) fenxizhan.elementAt(0);
                            fenxizhan.removeElementAt(0);
                        } else {
                            javax.swing.JOptionPane.showMessageDialog(null, "第" + hang + "行   error!");
                            fenxizhan.removeElementAt(0);
                            System.exit(0);
                        }
                        break;
                    }
                    case 8: {
                        if (judge == true || isnum == true) {
                            s = (String) fenxizhan.elementAt(0);
                            fenxizhan.removeElementAt(0);
                            fenxizhan.add(0, "Fchar");
                            fenxizhan.add(1, " 符号 ");
                            judge = false;
                            isnum = false;
                        } else if (s_wait.equals("(")) {
                            s = (String) fenxizhan.elementAt(0);
                            fenxizhan.removeElementAt(0);
                            fenxizhan.add(0, "Fchar");
                            fenxizhan.add(1, " 符号 ");
                        } else if (s_wait.equals("=")) {
                            s = (String) fenxizhan.elementAt(0);
                            fenxizhan.removeElementAt(0);
                            fenxizhan.add(0, "Fchar");
                            fenxizhan.add(1, " 符号 ");
                        } else {
                            javax.swing.JOptionPane.showMessageDialog(null, "第" + hang + "行   error!");
                            fenxizhan.removeElementAt(0);
                            System.exit(0);
                        }
                        break;
                    }
                    case 9: {
                        if (s_wait.equals("+")) {
                            s = (String) fenxizhan.elementAt(0);
                            fenxizhan.removeElementAt(0);
                        } else if (s_wait.equals("-")) {
                            s = (String) fenxizhan.elementAt(0);
                            fenxizhan.removeElementAt(0);
                        } else if (s_wait.equals(">")) {
                            s = (String) fenxizhan.elementAt(0);
                            fenxizhan.removeElementAt(0);
                        } else if (s_wait.equals("<")) {
                            s = (String) fenxizhan.elementAt(0);
                            fenxizhan.removeElementAt(0);
                        } else if (s_wait.equals("*")) {
                            s = (String) fenxizhan.elementAt(0);
                            fenxizhan.removeElementAt(0);
                            fenxizhan.add(0, "*");
                            fenxizhan.add(1, "Fchar");
                            fenxizhan.add(2, " 符号 ");
                        } else if (s_wait.equals("/")) {
                            s = (String) fenxizhan.elementAt(0);
                            fenxizhan.removeElementAt(0);
                            fenxizhan.add(0, "/");
                            fenxizhan.add(1, "Fchar");
                            fenxizhan.add(2, " 符号 ");
                        } else if (s_wait.equals(")")) {
                            s = (String) fenxizhan.elementAt(0);
                            fenxizhan.removeElementAt(0);
                        } else if (s_wait.equals(";")) {
                            s = (String) fenxizhan.elementAt(0);
                            fenxizhan.removeElementAt(0);
                        } else if (s_wait.equals("$")) {
                            s = (String) fenxizhan.elementAt(0);
                            fenxizhan.removeElementAt(0);
                        } else {
                            fenxizhan.removeElementAt(0);
                            javax.swing.JOptionPane.showMessageDialog(null, "第" + hang + "行   error11（非法运算符或缺少分号）!");
                            System.exit(0);
                        }
                        break;
                    }
                    case 10: {
                        if (judge == true || isnum == true) {
                            s = (String) fenxizhan.elementAt(0);
                            fenxizhan.removeElementAt(0);
                            fenxizhan.add(0, "变量");
                            judge = false;
                            isnum = false;
                        } else if (s_wait.equals("(")) {
                            s = (String) fenxizhan.elementAt(0);
                            fenxizhan.removeElementAt(0);
                            fenxizhan.add(0, "(");
                            fenxizhan.add(1, " 表达式");
                            fenxizhan.add(2, ")");
                        } else if (s_wait.equals("=")) {
                            s = (String) fenxizhan.elementAt(0);
                            fenxizhan.removeElementAt(0);
                            fenxizhan.add(0, "=");
                            fenxizhan.add(1, " 表达式");

                        } else {
                            fenxizhan.removeElementAt(0);
                            javax.swing.JOptionPane.showMessageDialog(null, "第" + hang + "行   error!");
                            System.exit(0);
                        }
                        break;
                    }
                }
            }
            System.out.println();
            System.out.print("分析栈:");
            jta.append("分析栈:");
            for (int i = 0; i < fenxizhan.size(); i++) {
                System.out.print(fenxizhan.elementAt(i));
                jta.append((String) fenxizhan.elementAt(i));
            }
            System.out.print("\t");
            jta.append("\t");
            System.out.print("待分析:");
            jta.append("待分析:");
            for (int j = 0; j < wait.size(); j++) {
                System.out.print(wait.elementAt(j));
                jta.append((String) wait.elementAt(j));
            }
            System.out.print("\t");
            jta.append("\n");
        }
    }

    public void word() {
        Baoliuzi.add("if");
        Baoliuzi.add("else");
        Baoliuzi.add("int");
        Baoliuzi.add("char");
        Baoliuzi.add("float");
        Baoliuzi.add("double");
        Baoliuzi.add("string");
        int i = 0;
        while (i < (wait.size() - 1)) {
            String name = (String) wait.elementAt(i);
            if (Baoliuzi.contains(name)) {
                System.out.println(name + "       " + "为 保留字");
                i++;
            } else if (jiefu.contains(name)) {
                System.out.println(name + "       " + "为 界符");
                i++;
            } else if (yunsuanfu.contains(name)) {
                System.out.println(name + "       " + "为 运算符");
                i++;
            } else if (isNum(name)) {
                System.out.println(name + "       " + "为 数字");
                i++;
            } else {
                System.out.println(name + "       " + "为 标识符");
                i++;
            }
        }
    }

    public void four() {
        String a = "";
        int i = 0;
        while (i < wait1.size()) {
            String linshi1 = "";
//			System.out.println(wait1.elementAt(i));
            String linshi = (String) wait1.elementAt(i);
            if (i < (wait1.size() - 1)) {
                linshi1 = (String) wait1.elementAt(i + 1);
            }
            if (linshi.equals("if")) {
                if (((String) wait1.elementAt(i + 3)).equals(">")
                        || ((String) wait1.elementAt(i + 3)).equals("<")
                        || ((String) wait1.elementAt(i + 3)).equals("=")) {

                    sys[count].fuhao = (String) wait1.elementAt(i + 3);
                    System.out.println((String) wait1.elementAt(i + 3));
                    sys[count].op1 = (String) wait1.elementAt(i + 2);
                    System.out.println((String) wait1.elementAt(i + 2));
                    sys[count].op2 = (String) wait1.elementAt(i + 4);
                    System.out.println((String) wait1.elementAt(i + 4));
                    huitian[sign] = count + 2;
                    System.out.println("huitian[sign]"+ String.valueOf(count+2));
                    System.out.println(huitian[sign]);
                    sign++;
                    sys[count].node.num = count + 2;
                    count++;
                    sys[count].fuhao = "j";
                    sys[count].op1 = "null";
                    sys[count].op2 = "null";
                    else_sign = count;
                    count++;
                    i++;
                }
            } else if (linshi.equals("else")) {

                sys[count].fuhao = "j";
                sys[count].op1 = "null";
                sys[count].op2 = "null";

                if_sign = count;
                count++;
                huitian[sign] = count;
                sign++;
                sys[else_sign].node.num = count;
                i++;
            } else if (linshi.equals("while")) {

                if (((String) wait1.elementAt(i + 3)).equals(">")
                        || ((String) wait1.elementAt(i + 3)).equals("<")
                        || ((String) wait1.elementAt(i + 3)).equals("=")) {

                    while_sign = count;
                    sys[count].fuhao = (String) wait1.elementAt(i + 3);
                    System.out.println((String) wait1.elementAt(i + 3));
                    sys[count].op1 = (String) wait1.elementAt(i + 2);
                    System.out.println((String) wait1.elementAt(i + 2));
                    sys[count].op2 = (String) wait1.elementAt(i + 4);
                    System.out.println((String) wait1.elementAt(i + 4));


                    huitian[sign] = count + 2;
                    sign++;
                    sys[count].node.num = count + 2;
                    count++;
                    sys[count].fuhao = "j";
                    sys[count].op1 = "null";
                    sys[count].op2 = "null";
                    while_endsign = count;
                    System.out.println(count);
                    System.out.println(while_endsign);
                    count++;
                    i++;
                }
            } else if (((String) wait1.elementAt(i)).equals("}") && linshi1.equals("else")) {
                i++;
            } else if (((String) wait1.elementAt(i)).equals("}") && (if_sign != 0)) {
                huitian[sign] = count;
                sign++;
                sys[if_sign].node.num = count;
                if_sign = 0;
                i++;
            } else if (linshi1.equals("=")) {
                if (((String) wait1.elementAt(i + 5)).equals(";")) {
                    sys[count].fuhao = (String) wait1.elementAt(i + 3);
                    sys[count].op1 = (String) wait1.elementAt(i + 2);
                    sys[count].op2 = (String) wait1.elementAt(i + 4);
                    sys[count].node.result = (String) wait1.elementAt(i);
                    count++;


                    i++;
                } else if (((String) wait1.elementAt(i + 3)).equals(";")) {
                    sys[count].fuhao = (String) wait1.elementAt(i + 1);
                    sys[count].op1 = (String) wait1.elementAt(i + 2);
                    sys[count].op2 = "null";
                    sys[count].node.result = (String) wait1.elementAt(i);
                    count++;
                    i++;
                } else if (((String) wait1.elementAt(i + 7)).equals(";")) {
                    if (((String) wait1.elementAt(i + 3)).equals("+")
                            || ((String) wait1.elementAt(i + 3)).equals("-")) {
                        sys[count].fuhao = (String) wait1.elementAt(i + 5);
                        sys[count].op1 = (String) wait1.elementAt(i + 4);
                        sys[count].op2 = (String) wait1.elementAt(i + 6);
                        sys[count].node.result = (String) ("t" + count);
                        count++;
                        sys[count].fuhao = (String) wait1.elementAt(i + 3);
                        sys[count].op1 = (String) wait1.elementAt(i + 2);
                        sys[count].op2 = (String) sys[count - 1].node.result;
                        sys[count].node.result = (String) wait1.elementAt(i);
                        count++;
                    } else {
                        sys[count].fuhao = (String) wait1.elementAt(i + 3);
                        sys[count].op1 = (String) wait1.elementAt(i + 2);
                        sys[count].op2 = (String) wait1.elementAt(i + 4);
                        sys[count].node.result = "t" + count;
                        count++;
                        sys[count].fuhao = (String) wait1.elementAt(i + 5);
                        sys[count].op1 = (String) wait1.elementAt(i + 6);
                        sys[count].op2 = sys[count - 1].node.result;
                        sys[count].node.result = (String) wait1.elementAt(i);
                        count++;
                    }
                    i++;
                }
            } else if (((String) wait1.elementAt(i)).equals("}") && (while_endsign != 0)) {
                sys[count].fuhao = "j";

                sys[count].op1 = "null";
                sys[count].op2 = "null";

                huitian[sign] = while_sign;
                sign++;
                sys[count].node.num = while_sign;
                count++;
                huitian[sign] = count;
                sign++;
                sys[while_endsign].node.num = count;
                while_endsign = 0;
                i++;
            } else
                i++;
        }
    }
}